<?php
include("../../tools/session.php");

// 如果登录成功
if ($is_login_success === true)
{
?>
<?php
	include("../../tools/mysql.php");
	db_connect();
   
	$company = array();
   
	// $sql = "SELECT * FROM view_comp_sec_pos WHERE position_id<>0 AND section_id<>0 AND company_id<>0 ORDER BY position_id ASC";
	$sql = "SELECT a.company_id company_id, a.zh_name company_name, a.power company_power,
				b.section_id section_id, b.zh_name section_name, b.master_id master_id, b.power section_power, 
				c.position_id position_id, c.zh_name position_name, c.power position_power
			FROM company a, section b, position c
			WHERE a.company_id = b.company_id AND b.section_id = c.section_id
					 AND a.company_id<>0 AND b.section_id<>0 AND c.position_id<>0
			ORDER BY company_id, section_id, position_id ASC";
	$re = db_query($sql);
   
	foreach ($re as $v)
	{
		$c_id			= $v["company_id"];
		$c_name		= $v["company_name"];
		$c_power		= $v["company_power"];
		$s_id			= $v["section_id"];
		$s_name		= $v["section_name"];
		$s_power		= $v["section_power"];
		$s_masterid	= $v["master_id"];
		$p_id			= $v["position_id"];
		$p_name		= $v["position_name"];
		$p_power		= $v["position_power"];
		
		// $c_temp 是一条“公司”的数组信息，其中第 [0] 项为指向“部门”数组的数组
		// $s_temp 是一条“部门”的数组信息，其中第 [0] 项为指向“职位”数组的数组
		// $p_temp 是一条“职位”的数组信息
		$c_temp = array(array(), $c_name, $c_power);
		$s_temp = array(array(), $s_name, $s_power, $s_masterid);
		$p_temp = array($p_name, $p_power);
		
		if (!isset($company[$c_id])) { $company[$c_id] = $c_temp; }
		if (!isset($company[$c_id][0][$s_id])) { $company[$c_id][0][$s_id] = $s_temp; }
		if (!isset($company[$c_id][0][$s_id][0][$p_id])) { $company[$c_id][0][$s_id][0][$p_id] = $p_temp; }
	}
	 // print_r($company);
	 // exit();
   
	header("content-type: text/xml");
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
	echo "<root>\n";
	foreach ($company as $k1 => $v1)
	{
		echo "<company id=\"" . $k1 . "\" name=\"" . $v1[1] . "\" power=\"" . $v1[2] . "\">";
		foreach ($v1[0] as $k2 => $v2)
		{
			echo "<section id=\"" . $k2 . "\" name=\"" . $v2[1] . "\" power=\"" . $v2[2] . "\" masterid=\"" . $v2[3] . "\">";
			foreach ($v2[0] as $k3 => $v3)
			{
				echo "<position id=\"" . $k3 . "\" name=\"" . $v3[0] . "\" power=\"" . $v3[1] . "\"></position>";
			}
			echo "</section>";
		}
		echo "</company>";
	}
	echo "</root>\n";
?>
<?php
}
// 如果登录失败或还未登录
else
{
   session_hop_page("../index.php");
   exit();
}
	  
?>
